package com.justrun.utils;

import com.alibaba.excel.EasyExcel;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Excel文件解析工具类
 * @author Chandler
 * @date 2020-09-24
 */
public class ExcelUtils {
    /**
     * 返回excel文件的第一个sheet的所有数据行
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static List<List<String>> getRowsFromExcel(InputStream inputStream) throws IOException {
        HSSFWorkbook workBook = new HSSFWorkbook(inputStream);
        HSSFSheet sheet = workBook.getSheetAt(0); // 获取第一个sheet
        List<List<String>> result = new ArrayList<List<String>>();
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            int cloNumber = sheet.getRow(i).getLastCellNum();
            List<String> list = new ArrayList<String>();
            for (int j = 0; j < cloNumber; j++) { // 遍历当前行的所有cell
                switch (sheet.getRow(i).getCell(j).getCellType()) {
                    case HSSFCell.CELL_TYPE_STRING:
                        list.add(sheet.getRow(i).getCell(j).getStringCellValue()); // 如果是字符串则保存
                        break;
                    case HSSFCell.CELL_TYPE_NUMERIC:
                        list.add(sheet.getRow(i).getCell(j).getNumericCellValue()+""); //将数值转换为字符串
                        break;
                    case HSSFCell.CELL_TYPE_BLANK:
                        break;
                }
            }
            result.add(list);
        }
        return result;
    }



}
